package com.region.integral.business_user.obtain.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.business_user.obtain.dao.IIntegralReplyConsultting;
import com.region.integral.business_user.pojo.BUIEntity;
import com.region.integral.business_user.xml.Regulation;
import com.region.integral.business_user.xml.dto.ReplyConsultting;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.BusinessUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iIntegralReplyConsultting")
public class IntegralReplyConsulttingImpl extends BaseHibernate implements IIntegralReplyConsultting {

	@Override
	public void replyConsultting(int userid, IntegralData integralData)
			throws IntegralException {
		BUIEntity buiEntity = getBUIEntity(userid);
		if (buiEntity != null) {
			int replyConsulttingNum = buiEntity.getReplyConsulttingNum();
			Regulation regulation = XMLRegulationUtil.unmarshaller2();
			List<ReplyConsultting> replyConsulttings = regulation.getReplyconsultting();
			for (ReplyConsultting replyConsultting : replyConsulttings) {
				if(replyConsulttingNum!=0 && replyConsulttingNum%replyConsultting.getRate()==0){
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ replyConsultting.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid, replyConsultting.getIntegral(), "replyConsultting"));
				}
			}
			buiEntity.setReplyConsulttingNum(replyConsulttingNum+1);
			hibernateTemplate.update(buiEntity);
		}
		
	}

}
